$(function () {

    //绑定回车
    $(document).keydown(function (e) {
        if (e.keyCode === 13) {
            signIN();
        }
    });

    //登录按钮
    $('#fs-login-btn').click(
        function () {
            signIN();
        }
    );

    //注册按钮
    $('#fs-regsiter-btn').click(
        function () {
            $('#regBox').modal({
                onHide: function () {
                    $('#regForm').find('input').val('');
                }
            }).modal('setting', 'transition', 'horizontal flip').modal('show');
        }
    );

    //关闭注册按钮
    $('#fs-close-register-btn').click(
        function () {
            $('#regBox').modal('hide');
            $("#username").focus();
        }
    );
    /**
     * 初始化FS的登录背景图片
     */
    var initBackgroundImage = function () {
        if ($('body').length > 0) {
            var loginImg = $('img.fs-login-img');
            loginImg.attr("src", "/static/fr/images/login_min.jpg");
            loginImg.css({
                width: document.body.clientWidth + "px",
                height: document.body.clientHeight + "px"
            });
        }
    };

    /**
     *初始化logo
     */
    var initLogoImage = function () {
        if ($('body').length > 0) {
            $("#fs-login-logo")[0].style.background = 'url("/static/fr/images/logo.png") no-repeat 50% 0px';
            $("#fs-login-logo")[0].style.backgroundSize = '120px 45px';
        }
    };

    var signIN = function () {
        var user = $("#username").val();
        var pw = $("#password").val();
        if (!user) {
            toastr.warning("用户名不能为空!");
            return
        }
        if (!pw) {
            toastr.warning("密码不能为空!");
            return
        }
        $.ajax({
            url: "/account/doLogin",
            type: "post",
            data: "loginName=" + user + "&password=" + pw,
            success: function (text) {
                var json = eval("(" + text + ")");
                if (json.success) {
                    window.location.href = "/index";
                } else {
                    $("#password").val("");
                    toastr.error(json.message);
                }
            },
            error: function () {
                toastr.error('网络错误，请重新尝试 …');
            }
        })
    };
    initBackgroundImage();
    initLogoImage();
    $("#username").focus();
    $('img.fs-login-img').css({
        "margin-left": "-" + 5 + "px",
        // "margin-top": "-" + 5 + "px",
        width: document.body.clientWidth + "px",
        height: document.body.clientHeight + "px"
    });
    $(window).resize(function () {
        $('img.fs-login-img').css({
            "margin-left": "-" + 5 + "px",
            "margin-top": "-" + 5 + "px",
            width: document.body.clientWidth + "px",
            height: document.body.clientHeight + "px"
        });
    });
    $('#regForm').form({
        on: 'blur',
        fields: {
            mobile: 'regExp[/^1[3|4|5|7|8][0-9]{9}$/]',
            email: 'regExp[/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/]',
            regPad: ['minLength[6]', 'maxLength[16]', 'empty'],
            rePassword: ['match[regPad]', 'empty'],
            agreement: 'checked',
            company:['empty'],
            username1:['empty'],
        },
        onSuccess: function (event, fields) {
            //显示遮罩层
            $('#regBox .ui.dimmable').dimmer({closable: false}).dimmer('show');
            $.ajax({
                url: "/account/register",
                type: "post",
                dataType: "json",
                data: "loginName=" + fields["email"] + "&password=" + fields["password"] + "&rePassword=" + fields["rePassword"] + "&email=" + fields["email"] +"&company=" + fields["company"]+"&name=" + fields["username1"]+"&tel=" + fields["tel"],
                success: function (res) {
                    if (res.success) {
                        toastr.success("恭喜您，申请注册成功，等待管理员审核！");
                        $('#regBox').modal('hide');
                        // window.location.href = "/";
                    } else {
                        toastr.error(res.message);
                        $('#regBox .ui.dimmable').dimmer('hide');
                    }
                },
                error: function () {
                    $('#regBox .ui.dimmable').dimmer('hide');
                    toastr.error('网络错误，请重新尝试 …');
                }
            })
        }
    });

});
